<?php
$uid = Session::get('mainuid');
$ua = new Model('user');
$ua->find($uid);

/**$mp = new Model('user_price');
$prc_arr = $mp->where("id > '1'")->map_array('id', 'price');*/

//所属代理商
$ma = new Model('user_agency');
if($ua->agid){
	$ma->find($ua->agid);
}else{
	$ma->find(array('un'=>'admin'));
} 


// $mp = new Model('user_setmeal');
// $mp->find(array('user_agency_id'=>$ma->id));
$mp = get_user_setmeal($ma->id);

$prc_arr = json_decode($mp->price,true);
if(\Request::post("price_id")){
	die($prc_arr[\Request::post("price_id")]);
}
// if($mp->id){
// 	$prc_arr = json_decode($mp->price,true);
// 	unset($prc_arr['1']);
	
// }else{
// 	$mp->find(array('isadmin'=>1));
// 	if($mp->id){
// 		$prc_arr = json_decode($mp->price,true);
// 		unset($prc_arr['1']);
// 	}else{
// 		$prc_arr = array('2'=>2400,'3'=>4800,'4'=>7200,'5'=>9600,'6'=>12000,'7'=>12000,'8'=>12000);
// 		//$prc_arr = array('1'=>2400,'2'=>4800,'3'=>7200,'4'=>9600,'5'=>12000,'6'=>12000,'7'=>12000);
// 	}
// }

$m = new SampleModel();
$rank_arr = translate_level();
// unset($rank_arr['1']);
$ys = array();
for($i=0;$i<37;$i++){
	$ys[$i] = $i;
}


if(myStrtotime($ua->mendtime)>time() && $ua->level_id!=1){
$day_price=$prc_arr[$ua->level_id]/365;//当前会员日价格（省略小数）
$day_time=floor((myStrtotime($ua->mendtime)-time())/86400);//当前会员剩余天数（省略小数）
$return_price=$day_time*$day_price;//当前会员折扣金钱数（省略小数）	
$check=1;
}else{
	$check=0;
	$day_price=0;
	$day_time=0;
	$return_price=0;
}

if(Request::post()){
	$ys = intval(Request::post('ys'));
	$lx = Request::post('lx');
	$myfy = intval($prc_arr[$lx]);
	if(myStrtotime($ua->mendtime)<time()){
		//已经到期
		if(!$ys){
			die('no');
		}
		$hf = $myfy*$ys;
		$wdye = intval($ua->balance);
		if($wdye<$hf){
		die();
		}elseif(intval($ma->yue)<$hf){
		die('tno');	
		}else{
		//进行充值
			$red = new Model('agency_pay_record');
			$red->uid = $ua->id;
			$red->pid = $ma->id;
			$red->money = $hf;
			$red->months = $ys;
			$red->level_id = $lx;
			$red->cwid = $ua->cwid;
			$red->un = $ua->un;
			$red->bz = Request::post('bz');
			$red->btime = DB::raw('now()');
			$red->isyg = $ma->isyg;
			if($red->save()){
				$ma->yue = intval($ma->yue)-deduction(1,$ys,$lx);
				$ua->balance=intval($ua->balance)-$hf;
				$ua->level_id=$red->level_id;
				//$ua->mendtime=date('Y-m-d H:i:s',strtotime('+'.$red->months." year"));
				$ua->mendtime= formatDateByTimeStamp(myStrtotime('',$red->months));

				$ma->save();
				$ua->save();	
			}
		Response::write('ok');
		}	
	}else{
		//未到期
		if($lx<=$ua->level_id){
			if(!$ys){
			die('no');
			}
			$hf = $myfy*$ys;
			$wdye = intval($ua->balance);
				if($wdye<$hf){
					die();
				}elseif(intval($ma->yue)<$hf){
					die('tno');	
				}else{
				//进行充值
				$red = new Model('agency_pay_record');
				$red->uid = $ua->id;
				$red->pid = $ma->id;
				$red->money = $hf;
				$red->months = $ys;
				$red->level_id = $lx;
				$red->cwid = $ua->cwid;
				$red->un = $ua->un;
				$red->bz = Request::post('bz');
				$red->btime = DB::raw('now()');
				$red->isyg = $ma->isyg;
				if($red->save()){
				$ma->yue = intval($ma->yue)-deduction(1,$ys,$lx);
				$ua->balance=intval($ua->balance)-$hf;
				$ua->level_id=$red->level_id;
				//$ua->mendtime=date('Y-m-d H:i:s',strtotime('+'.$red->months." year",strtotime($ua->mendtime)));
				$ua->mendtime = formatDateByTimeStamp(myStrtotime($ua->mendtime,$red->months));

				$ma->save();
				$ua->save();	
				}
				Response::write('ok');
				}	
		}else if($lx>$ua->level_id){
			$day_price=floor($prc_arr[$ua->level_id]/365);//当前会员日价格（省略小数）
			$now_time=floor((myStrtotime($ua->mendtime)-time())/86400);//当前会员剩余天数（省略小数）
			$now_price=$now_time*$day_price;//当前会员折扣金钱数（省略小数）
			$days_price=floor($prc_arr[$lx]/365);//续费会员等级日价格（省略小数）
			$tow_price=$now_time*$days_price;//续费会员等级金钱数（省略小数）
			$price=$tow_price-$now_price;//还需要补的钱数
			$hf = $myfy*$ys+$price;//总价
			$wdye = intval($ua->balance);
				if($wdye<$hf){
					die();
				}elseif(intval($ma->yue)<$hf){
					die('tno');	
				}else{
				//进行充值
				$red = new Model('agency_pay_record');
				$red->uid = $ua->id;
				$red->pid = $ma->id;
				$red->money = $hf;
				$red->months = $ys;
				$red->level_id = $lx;
				$red->cwid = $ua->cwid;
				$red->un = $ua->un;
				$red->bz = Request::post('bz');
				$red->btime = DB::raw('now()');
				$red->isyg = $ma->isyg;
				if($red->save()){
				$ma->yue = intval($ma->yue)-deduction(2,$ys,$lx);
				$ua->balance=intval($ua->balance)-$hf;
				$ua->level_id=$red->level_id;
				//$ua->mendtime=date('Y-m-d H:i:s',strtotime('+'.$red->months." year",strtotime($ua->mendtime)));
				$ua->mendtime = formatDateByTimeStamp(myStrtotime($ua->mendtime,$red->months));

				$ma->save();
				$ua->save();	
				}
				Response::write('ok');
				}	
		}
	
	}
}
//代理商应该扣除的价格
function deduction($id,$ys,$lx){
$userp = new Model('user_prices');
$userp->find(array('isadmin'=>1));
if($userp->id){
	$prc_arr = json_decode($userp->price,true);
	unset($prc_arr['1']);
}else{
	$prc_arr = array('2'=>2400,'3'=>4800,'4'=>7200,'5'=>9600,'6'=>12000,'7'=>12000,'8'=>12000);
	//$prc_arr = array('1'=>2400,'2'=>4800,'3'=>7200,'4'=>9600,'5'=>12000,'6'=>12000,'7'=>12000);
}
if($id==1){
	$myfy = intval($prc_arr[$lx]);
	return $myfy*$ys;
}else if($id==2){
	global $ua;
	$myfy = intval($prc_arr[$lx]);
	$day_price=floor($prc_arr[$ua->level_id]/365);//当前会员日价格（省略小数）
	$now_time=floor((myStrtotime($ua->mendtime)-time())/86400);//当前会员剩余天数（省略小数）
	$now_price=$now_time*$day_price;//当前会员折扣金钱数（省略小数）
	$days_price=floor($prc_arr[$lx]/365);//续费会员等级日价格（省略小数）
	$tow_price=$now_time*$days_price;//续费会员等级金钱数（省略小数）
	$price=$tow_price-$now_price;//还需要补的钱数
	$hf = $myfy*$ys+$price;//总价
	return $hf;
}
}

//根据给定的时间戳格式化为给定的时间
function formatDateByTimeStamp($timeStamp){
	$datetime = new DateTime("@{$timeStamp}");
	$datetime->setTimezone(new DateTimeZone('Asia/Shanghai'));
	return $datetime->format('Y-m-d H:i:s');
}

function myStrtotime($nowTime='',$ad=0,$type='Y'){
	$datetime = new \DateTime($nowTime);
	$interval = new \DateInterval("P{$ad}{$type}");
	$datetime->add($interval);
    return $datetime->format('U');
}
//获取代理用户套餐, 如果当前代理没权限, 往上级代理找
function get_user_setmeal($user_agency_id){
	$setmeal = (new Model('user_setmeal'))->find(['user_agency_id'=>$user_agency_id]);
	if($setmeal->id){
		return $setmeal;
	}
	$user_agency = (new Model('user_agency'))->find($user_agency_id);
	$setmeal = get_user_setmeal($user_agency->pid);
	return $setmeal;
}